<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class VideoController extends BaseController {

	//ThinkPHP的构造函数
	public function _initialize() {
		//自动加载的类
		parent::_initialize();
		new \Org\Admin_autoload();
	}

    public function index(){
		header("Content-type: text/html; charset=utf-8");
    	$this->assign('page_title','视频列表');
    	$Model = D("Video");
    	$list=$Model->select();
		//dump($list);exit;
    	$this->assign('volist',$list);
    	
    	//$this->assign('')
        $this->display();
    }
    
    public function add(){
		define('ROOT_PATH', dirname(THINK_PATH) .'/www/' );
		//echo ROOT_PATH;exit;
    	if(!empty($_POST)) {
			//判断是否上传了视频或图片
			if(!empty($_FILES['imgfile']['tmp_name'])||!empty($_FILES['imgarr']['tmp_name'])){
				$root_path=ROOT_PATH.'Public/Uploads';
				//echo $root_path;exit;
				if(!is_dir($root_path)){
					mkdir($root_path);
				}
				$root_path=ROOT_PATH.'Public/Uploads/Video';
				if(!is_dir($root_path)){
					mkdir($root_path);
				}
				//echo $root_path;exit;
				//上传图片
				$upload= new \Think\Upload();// 实例化上传类
				$upload->maxSize=500048000;// 设置附件上传大小 500M
				$upload->exts=array();// 设置附件上传类型
				$upload->rootPath=$root_path.'/'; // 设置附件上传根目录
				$upload->savePath=''; // 设置附件上传（子）目录
				$upload->subName='';
				$upload->saveName='uniqid';
				$info=$upload->upload();
				//dump($info);exit;
				if(!$info) {// 上传失败
					$this->error ( $upload->getErrorMsg () );
				}
				if($info['imgfile']['savename']!=''){
					$_POST['imgurl']='/Public/Uploads/Video/'.$info['imgfile']['savename'];
				}
				if($info['imgarr']['savename']!=''){
					$_POST['imgarr']='/Public/Uploads/Video/'.$info['imgarr']['savename'];
				}
			}
			$NewsModel = D("Video");
			$_POST['create_time'] = date('Y-m-d H:i:s');
			$_POST['is_show'] = 1;
			$_POST['hit'] = 0;
			$data = $NewsModel->create($_POST);
			$insert_id = $NewsModel->add($data);
			if($insert_id){

				header("Content-type: text/html; charset=utf-8");
				echo "<script>window.location.href='/Video/';</script>";exit;
			}
    	}else{
    	    //$CateModel = D("NewsCategory");
    	    //$category_list = $CateModel->where("ntype=5")->select();
    		//$this->assign('category_list',$category_list);
    	    $this->display();
    	}
    }


    public function getData(){
    	$DataModel = D("Video");
    	$list = $DataModel->select();
    	//$CateModel = D("NewsCategory");
    	//$category_list = $CateModel->where('ntype=5')->select();
    	//$categories = array();
    	//foreach($category_list as $v){
    		//$categories[$v['id']] = $v['name'];
    	//}
    	//for($i=0;$i<count($list);$i++){
    		//$list[$i]['category'] = $categories[$list[$i]['category_id']];
    	//}
    	$this->ajaxReturn($list);
    }
    
    
    public function edit(){
		header("Content-type: text/html; charset=utf-8");
		define('ROOT_PATH', dirname(THINK_PATH) .'/www/' );
    	$DataModel = D("Video");
    	if(!empty($_POST)) {
			//判断是否上传了视频或图片
			if(!empty($_FILES['imgfile']['tmp_name'])||!empty($_FILES['imgarr']['tmp_name'])){
				$root_path=ROOT_PATH.'Public/Uploads';
				//echo $root_path;exit;
				if(!is_dir($root_path)){
					mkdir($root_path);
				}
				$root_path=ROOT_PATH.'Public/Uploads/Video';
				if(!is_dir($root_path)){
					mkdir($root_path);
				}
				//echo $root_path;exit;
				//上传图片
				$upload= new \Think\Upload();// 实例化上传类
				$upload->maxSize=500048000;// 设置附件上传大小 500M
				$upload->exts=array();// 设置附件上传类型
				$upload->rootPath=$root_path.'/'; // 设置附件上传根目录
				$upload->savePath=''; // 设置附件上传（子）目录
				$upload->subName='';
				$upload->saveName='uniqid';
				$info=$upload->upload();
				//dump($info);exit;
				if(!$info) {// 上传失败
					$this->error ( $upload->getErrorMsg () );
				}
				if($info['imgfile']['savename']!=''){
					$_POST['imgurl']='/Public/Uploads/Video/'.$info['imgfile']['savename'];
				}
				if($info['imgarr']['savename']!=''){
					$_POST['imgarr']='/Public/Uploads/Video/'.$info['imgarr']['savename'];
				}

			}
			//dump($_POST);exit;
    		$DataModel->where("id=".(int)$_POST['id'])->save($_POST);
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>window.location.href='/Video/';</script>";exit;
    	}else{
    		$item = $DataModel->where("id=".(int)$_GET['id'])->select();
			$item[0]['root_path']=ROOT_PATH;
			//dump($item[0]);exit;
    		$this->assign('vo',$item[0]);
    		//$CateModel = D("NewsCategory");
    		//$category_list = $CateModel->where('ntype=5')->select();
    		//$this->assign('category_list',$category_list);
    		$this->display();
    	}
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D("Video");
    		$id = $_POST['id'];
    		$item = $DataModel->where("id=".(int)$id)->find();
    		$result = $DataModel->delete((int)$id);
    		
    		if($result){
    			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    			unlink(str_replace("http://".FRONT_SITE_NAME,$root_path."/www",$item['imgurl']));
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }
       
    
}